package com.service;
import com.dao.BookDao;
import com.dao.impl.AdminDaoImpl;
import com.dao.impl.BookDaoImpl;
import com.dao.AddDao;
import com.dao.impl.AddDaoImpl;
import com.entity.Book;

import java.util.Objects;
public class BookService {
    private BookDao bookDao;

    public BookService() {
        this.bookDao = new BookDaoImpl();
        addDao = new AddDaoImpl();
    }

    public boolean deleteBookById(long id) throws Exception {
        boolean b = bookDao.deleteBookById(id);
        return b;
    }
    private AddDao addDao;

    public Book save(Book book) throws Exception {
        Book dbbook = addDao.getByBookname(book.getBookname());
        //如果存在
        if (Objects.nonNull(dbbook)) {
//            throw new  IllegalStateException(String.format("已经存在用户名为%s的用户",user.getUsername()));
            dbbook.setBookcount(dbbook.getBookcount() + 1);
            addDao.updateByBook(dbbook);
        } else {
            addDao.addBook(book);
        }
        return dbbook;
    }

    public Book getBookById(long id) throws Exception {
        Book b = bookDao.getBookById(id);
        return b;
    }

    public Book updataBook(Book book,long id) throws Exception {
        Book b = bookDao.updataBook(book,id);
        return b;
    }
}



